package com.itheima.exerciseTest.method10_19.method16;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class StudentDemo {
    public static void main(String[] args) {
        HashMap<Student, String> hm = new HashMap<>();
        Student s1 = new Student("张三",23);
        Student s2 = new Student("李四",24);
        Student s3 = new Student("王五",25);
        hm.put(s1,"湖南");
        hm.put(s2,"四川");
        hm.put(s3,"广东");

        // 第一种遍历方式 keySet()
        // method1(hm);

        // 第二种遍历方式 entrySet()
        // method2(hm);

        // 第三种遍历方式 匿名内部类
        // method3(hm);

        // 第四种方法 Lambda函数式编程
        hm.forEach(StudentDemo::accept);

    }

    private static void method3(HashMap<Student, String> hm) {
        hm.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student key, String value) {
                System.out.println(key + "::" + value);
            }
        });
    }

    private static void method2(HashMap<Student, String> hm) {
        Set<Map.Entry<Student,String>> entries = hm.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "::" + value);
        }
    }

    private static void method1(HashMap<Student, String> hm) {
        Set<Student> keys = hm.keySet();
        for (Student key : keys) {
            String value = hm.get(key);
            System.out.println(key + "::" + value);
        }
    }

    private static void accept(Student key, String value) {
        System.out.println(key + "::" + value);
    }
}
